package com.siqi.hr.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 导入实体数据验证数据是否合法工具类
 * @author      xiaogq
 * @date        2017-01-10
 */
public class RegularExpressionValidation {

    /**
     * 验证varchar类型字段
     * @author      xiaogq
     * @param str   值
     * @param rule  验证规则
     * @return
     */
    public static boolean isVarchar(String str, String rule) {
        if (str != null && !str.trim().equals("")) {
            if (rule != null && !"".equals(rule.trim())) {
                Pattern pat = Pattern.compile(rule);
                Matcher mat = pat.matcher(str.trim());
                return mat.matches();
            }
            return true;
        }
        return true;
    }

    /**
     * 验证bit类型字段
     * @author      xiaogq
     * @param boo   值
     * @param rule  验证规则
     * @return
     */
    public static boolean isBit(String boo, String rule) {
        if (boo != null && !boo.trim().equals("")) {
            if (rule != null && !"".equals(rule.trim())) {
                Pattern pat = Pattern.compile(rule);
                Matcher mat = pat.matcher(boo.trim());
                return mat.matches();
            }
            return true;
        }

        return true;
    }

    /**
     * 验证datetime类型字段
     * @author           xiaogq
     * @param dateTime   值
     * @return
     */
    public static boolean isDatetime(String dateTime) {
        if (dateTime.trim() != null && dateTime.trim() != "" && !dateTime.trim().equals("")) {
            StringBuffer reg = new StringBuffer();
            reg.append("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\s]?((((0?[13578])|");
            reg.append("(1[02]))[\\-\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\s]?");
            reg.append("((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}");
            reg.append("(([02468][1235679])|([13579][01345789]))[\\-\\s]?((((0?[13578])|(1[02]))[\\-\\s]");
            reg.append("?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\s]?((0?[1-9])|([1-2][0-9])");
            reg.append("|(30)))|(0?2[\\-\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|(1?[0-9])|(2?[0-4]))");
            reg.append("\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
            Pattern pat = Pattern.compile(reg.toString());
            Matcher mat = pat.matcher(dateTime.trim());
            return mat.matches();
        }
        return true;
    }

    /**
     * 验证date类型字段
     * @author           xiaogq
     * @param date       值
     * @return
     */
    public static boolean isDate(String date) {
        if (date.trim() != null && date.trim() != "" && !date.trim().equals("")) {
            StringBuffer reg = new StringBuffer();
            reg.append("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?");
            reg.append("((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])");
            reg.append("|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))");
            reg.append("|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])");
            reg.append("|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])");
            reg.append("|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])");
            reg.append("|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))");
            Pattern pat = Pattern.compile(reg.toString());
            Matcher mat = pat.matcher(date.trim());
            return mat.matches();
        }
        return true;
    }

    /**
     * 验证double类型字段
     * @author           xiaogq
     * @param   dub      值
     * @param   rule     验证规则
     * @return
     */
    public static boolean isDouble(String dub, String rule) {
        if (dub != null && !dub.trim().equals("")) {
            if (rule != null && !"".equals(rule.trim())) {
                Pattern pat = Pattern.compile(rule);
                Matcher mat = pat.matcher(dub.trim());
                return mat.matches();
            }
            return true;
        }
        return true;
    }

    /**
     * 验证int类型字段
     * @author           xiaogq
     * @param   str      值
     * @param   rule     验证规则
     * @return
     */
    public static boolean isInt(String str, String rule) {
        if (str.trim() != null && !str.trim().equals("")) {
            if (rule != null && !"".equals(rule.trim())) {
                Pattern pat = Pattern.compile(rule);
                Matcher mat = pat.matcher(str.trim());
                return mat.matches();
            }
            return true;
        }
        return true;
    }

}
